package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.youdianda.common.CustomException;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.dto.DataDto;
import com.itheima.youdianda.entity.SysMenu;
import com.itheima.youdianda.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_menu")
@Slf4j
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 菜单管理信息展示
     * @return
     */
    @GetMapping("/allData")
    public R<List<DataDto>> allData(){
        List<DataDto> dataDtoList = new ArrayList<>();
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getPid,0);
        List<SysMenu> dataList = sysMenuService.list(wrapper);
        for (SysMenu menu : dataList) {
            // 创建一个空的dataDto对象,用于存取需要的dataDto数据
            DataDto dataDto = new DataDto();
            // 将符合条件的数据复制给dataDto
            BeanUtils.copyProperties(menu,dataDto);
            // 获取此条数据的type值用于查询children数据
//            Integer type = menu.getType();
            String path = menu.getPath();
            LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.likeRight(SysMenu::getComponent,path.substring(1));
//            queryWrapper.eq(SysMenu::getPid,type);
            List<SysMenu> childrenList = sysMenuService.list(queryWrapper);
            dataDto.setChildren(childrenList);
            dataDtoList.add(dataDto);
        }
        return R.success(dataDtoList);
    }

    /**
     * 添加新的菜单
     * @param sysMenu
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody SysMenu sysMenu){
        if (sysMenu==null){
            throw new CustomException("当前无任何数据,请检查");
        }
        sysMenu.setCreate_date(LocalDateTime.now());
        sysMenuService.save(sysMenu);
        return R.success("添加成功");
    }

    /**
     * 菜单数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<SysMenu> getForId(String id){
        SysMenu sysMenu = sysMenuService.getById(id);
        if (sysMenu==null){
            throw new CustomException("无当前数据");
        }
        return R.success(sysMenu);
    }

    /**
     * 菜单数据修改
     * @param sysMenu
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody SysMenu sysMenu){
        sysMenuService.updateById(sysMenu);
        return R.success("修改成功");
    }

    /**
     * 菜单数据删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> delete(String id){
        sysMenuService.removeById(id);
        return R.success("删除成功");
    }
}
